package org.wlldTest.lesson9;


import com.alibaba.fastjson.JSON;
import org.dromara.easyai.config.RZ;
import org.dromara.easyai.function.Tanh;
import org.dromara.easyai.nerveCenter.ModelParameter;
import org.dromara.easyai.nerveCenter.NerveManager;
import org.dromara.easyai.nerveEntity.SensoryNerve;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class Test {
    public static NerveManager nerveManager;
    public static MyBusiness myBusiness = new MyBusiness();

    public static void init() throws Exception {
        nerveManager = new NerveManager(2, 3, 2,
                1, new Tanh(), 0.05f, RZ.NOT_RZ, 0, 0,0.9f,2,false);
        nerveManager.init(true, true, true, myBusiness);
//        ModelParameter modelParameter = JSONObject.parseObject(MyModel.MyModel, ModelParameter.class);
//        nerveManager.insertDnnModel(modelParameter);
    }

    public static void main(String[] args) throws Exception {
        init();
        study();
        //test();
    }

    public static void test() throws Exception {
        Random random = new Random();
        for (int i = 0; i < 10; i++) {
            Back2 back = new Back2();
            System.out.println("=======================");
            float x1 = random.nextFloat() * 0.1f;
            float y1 = random.nextFloat() * 0.1f + 0.1f;
            float x2 = x1 + 0.5f;
            float y2 = y1 + 0.5f;
            float[] parameter1 = new float[]{x1, y1};
            float[] parameter2 = new float[]{x2, y2};
            insertParameter(parameter1, false, null, back);
            int id = back.getId();
            float value = back.getOut();
            System.out.println("第一组数据数据分类id为:" + id + ",概率为:" + value);
            back.clear();
            insertParameter(parameter2, false, null, back);
            id = back.getId();
            value = back.getOut();
            System.out.println("第二组数据数据分类id为:" + id + ",概率为:" + value);
        }
    }

    public static void study() throws Exception {
        Random random = new Random();
        Map<Integer, Float> E1 = new HashMap<>();//第一组数据期望
        Map<Integer, Float> E2 = new HashMap<>();//第二组数据期望
        E1.put(1, 1f);
        E2.put(2, 1f);
        for (int i = 0; i < 200; i++) {
            System.out.println("======================");
            float x1 = random.nextFloat() * 0.1f;
            float y1 = random.nextFloat() * 0.1f + 0.1f;
            float x2 = x1 + 0.5f;
            float y2 = y1 + 0.5f;
            float[] parameter1 = new float[]{x1, y1};
            float[] parameter2 = new float[]{x2, y2};
            insertParameter(parameter1, true, E1, null);
            insertParameter(parameter2, true, E2, null);
        }
        ModelParameter modelParameter = nerveManager.getDnnModel();
        String model = JSON.toJSONString(modelParameter);
        System.out.println(model);
    }

    public static void insertParameter(float[] parameter, boolean isStudy, Map<Integer, Float> E
            , Back2 back) throws Exception {
        List<SensoryNerve> sensoryNerves = nerveManager.getSensoryNerves();
        for (int i = 0; i < sensoryNerves.size(); i++) {
            sensoryNerves.get(i).postMessage(1, parameter[i], isStudy, E, back);
        }
    }
}
